(function(KKBus){

    function GiftsController() {
        var self = this;
        
        self.gifts = ko.observableArray();
        
        self.getGifts = function (page)
        {
            self.gifts.removeAll();
            
            app.request++;
        
            var url = 'rest/awards';
            
            if (page > 0) {
                
                
            }
        
        
        KKBus.Ajax.doGet(url, function(data, textStatus, jqXHR)
        {
           
        var jsData = JSON.parse(data);
        var i = jsData.length;
        
        
            while(i--){
                 self.gifts.push(new KKBus.GiftModel(jsData[i].id, jsData[i].name, jsData[i].description, jsData[i].points, jsData[i].quantity)); 
         }
         
         app.request--;
         
          if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " + GiftsController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
        };
        
        self.showMessage = ko.observable(false);
        self.message = ko.observable("---");
        self.orderGift = function(id, index) {
            var gift = app.Knockout.GiftsController.gifts()[index];
            var quantity = gift.quantity();
            var points = gift.points();

            var user = app.Knockout.AuthenticationController.user;
            var userId = user.id();
            var userPoints = user.points();

            if (userPoints >= points && quantity > 0) {
                var exchange = JSON.stringify({
                    awardId: id,
                    userId: userId
                });            

                KKBus.Ajax.doPost('rest/exchange', exchange, function(result, textStatus, jqXHR) {
                    if (result == "ADDED") {
                        gift.quantity(quantity - 1);
                        user.points(userPoints - points);
                        
                        self.message('Kod weryfikujący zamówienie został wysłany na twój e-mail!');
                        self.showMessage(true);
                        setTimeout(function() { 
                            self.message('---');
                            self.showMessage(false);
                        }, 5000);
                    }
                });
            } else {
                self.message('Nie posiadasz wystarczającej ilości punktów!');
                self.showMessage(true);
                setTimeout(function() { 
                    self.message('---');
                    self.showMessage(false);
                    }, 5000);
                }
            };
        
             self.deleteGifts = function(id, index) {
                
                
                self.gifts.remove(self.gifts()[index]);
//                console.log(id);
                KKBus.Ajax.doDelete('rest/award/'+id, function(result, data, textStatus, jqXHR) {
                    if (result == "DELETED") { 
                }  
            });   
        };    
        
            self.deleteAward = function(id, index) {
                  
                self.gifts.remove(self.gifts()[index]);  
                KKBus.Ajax.doDelete('rest/exchange/'+id, function(result, textStatus, jqXHR) {
            });   
        };    
        
        
       self.validation = ko.observable(false);
       self.errorMessage = ko.observable("Blad");
       self.success = ko.observable(false);  
        
       self.addGifts = function() {
       
            var gifts = JSON.stringify({
                    name: $('#formNamee').val(),
                    description: "<img src='"+$('#formObrazek').val()+"' alt=''>",
                    points: $('#formPoints').val(),
                    quantity: $('#formQuantity').val()
          
                 });
                 
                 
        if(KKBus.Validation.length($('#formNamee').val())
          && KKBus.Validation.length($('#formObrazek').val())
          && KKBus.Validation.length($('#formPoints').val())
          && KKBus.Validation.length($('#formQuantity').val())){ 
      
            if(KKBus.Validation.licz($('#formPoints').val())
               && KKBus.Validation.licz($('#formQuantity').val())){
      
                    if(KKBus.Validation.word($('#formNamee').val())){
                         
//                          if(KKBus.Validation.link($('#formObrazek').val())){
                              
                          
                                         KKBus.Ajax.doPost('rest/award/', gifts, function(result, textStatus, jqXHR) {
                                         if (result == "ADDED") {
                                             
                                              
                                              self.success(true);
                                              $('#formNamee').val(' ');
                                              $('#formObrazek').val(' ');
                                              $('#formPoints').val(' ');
                                              $('#formQuantity').val(' ');
                                              setTimeout(function() { 
                                               self.success(false);
                                                }, 3000);                                              
                                              
                                              var temp = JSON.parse(gifts);

                                          var GiftsAdd = new KKBus.GiftModel(temp.id,temp.name,temp.description,temp.points,temp.quantity);

                                          self.gifts.push(GiftsAdd);     
                                            
                                            
                                         }
                                        });
//                                  }else{
//                                     
//                                     self.validation(true);
//                                     self.errorMessage("Błędny format w polu dodania obrazka dla nagrody!");
//                                     setTimeout(function() { self.validation(false); }, 3000); 
//                                      
//                                };
                                
                         }else{
                             
                             self.validation(true);
                             self.errorMessage("Błędny format w polu z nazwą nagrody!");
                             setTimeout(function() { self.validation(false); }, 3000); 
                        
                        };
                        
                    }else{
                        
                       self.validation(true);
                       self.errorMessage("Błędny format w polu z punktami lub ilością nagrod w magazynie!");
                       setTimeout(function() { self.validation(false); }, 3000); 
                        
                    };
                           
               }else{
                                 
               self.validation(true);
               self.errorMessage("Puste pola!");
               setTimeout(function() { self.validation(false); }, 3000);
                                                     
              };
         
       };
      
      
             
        
    }
   

    KKBus.GiftsController = GiftsController;
    
}(window.KKBus));

